package com.accesshollywood.grfxOrdering.panels;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import com.accesshollywood.components.EditableListWithPopup.EditableListModel;
import com.accesshollywood.grfxOrdering.basics.Colors;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.grfxOrdering.orderTreeSpecial.models.AbstractButtonPlusModel;
import com.accesshollywood.grfxOrdering.panels.DragNDropPanel.DROP;
import com.accesshollywood.indexables.GrfxOrder;
import com.accesshollywood.indexables.items.GrfxItem;
import com.accesshollywood.indexables.items.GrfxItemList;

public class ItemListPanel extends JPanel {
	public GrfxOrder							grfxOrder;
	public ArrayList<AbstractButtonPlusModel>	allButtons		= new ArrayList<AbstractButtonPlusModel>();
	public ButtonListPanel						btnFilename;
	public JPanel								itemPanel;
	public JPanel								filePanel;
	public ItemTextPanel						textPanel;
	public DragNDropPanel						stillsPanel;
	public DragNDropPanel						footagePanel;

	// chckbxSecret only shows up if specific rights are available to user. Helps with
	// selecting things for editing or removal.
	//
	// send a class into secretClass, and its constructor will be called when
	// then checkbox is clicked. The constructor should accept a boolean
	// so the checkbox can tell it whether it is selected or not.
	//
	public JCheckBox							chckbxSecret;
	public ActionListener						secretListener;
	public boolean								useChckbxSecret	= false;

	/**
	 * @wbp.parser.constructor
	 */
	public ItemListPanel(GrfxOrder model) {
		this.grfxOrder = model;
		createPanel();
	}

	public ItemListPanel(GrfxOrder model, boolean useSecretCheck) {
		this.grfxOrder = model;
		this.useChckbxSecret = useSecretCheck;
		createPanel();
	}

	public ItemListPanel(GrfxOrder model, boolean useSecretCheck, ActionListener secretListener) {
		this.grfxOrder = model;
		this.useChckbxSecret = useSecretCheck;
		this.secretListener = secretListener;
		createPanel();
	}

	public void createPanel() {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		addItemTree(grfxOrder.getItems());
	}

	public void addItemTree(ArrayList<GrfxItem> currItems, boolean useSecretCheck) {
		useChckbxSecret = useSecretCheck;

		addItemTree(grfxOrder.getItems());
	}

	public void addItemTree(ArrayList<GrfxItem> currItems, boolean useSecretCheck, Class secretClass) {
		this.useChckbxSecret = useSecretCheck;
		this.secretListener = secretListener;

		addItemTree(grfxOrder.getItems());
	}

	public void addItemTree(GrfxItemList grfxItemList) {
		removeAll();
		setBackground(Color.BLACK);
		int i = 0;
//		for (Map.Entry<Integer, GrfxItem> entry : indexedSQL.entrySet()) {
		Iterator itr = grfxItemList.iterator();

		while(itr.hasNext()){
			GrfxItem currItem = (GrfxItem)itr.next();
			i++;

			itemPanel = new JPanel();
			itemPanel.setLayout(new BoxLayout(itemPanel, BoxLayout.Y_AXIS));
			itemPanel.setBorder(new TitledBorder(new LineBorder(Color.BLACK, 2), ""));
			itemPanel.setAlignmentX(LEFT_ALIGNMENT);
			if (i % 2 != 0) {
				itemPanel.setBackground(Colors.getColor(Colors.DEFAULT_ODD));
			} else {
				itemPanel.setBackground(Colors.getColor(Colors.DEFAULT_EVEN));
			}
			add(itemPanel);

			if (useChckbxSecret) {
				chckbxSecret = new JCheckBox(currItem.getId() + "");
				chckbxSecret.addActionListener(secretListener);
				itemPanel.add(chckbxSecret);
			}

			JPanel itemSubPanel = new JPanel();
			itemSubPanel.setLayout(new BoxLayout(itemSubPanel, BoxLayout.Y_AXIS));
			itemSubPanel.setAlignmentX(LEFT_ALIGNMENT);
			itemPanel.add(itemSubPanel);

			if (currItem.getFilename() != null && currItem.getFilename() != "") {
				filePanel = new JPanel();
				filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.X_AXIS));
				filePanel.setAlignmentX(LEFT_ALIGNMENT);
				itemPanel.add(filePanel);

//				btnFilename = new ButtonListPanel(grfxOrder, filePanel, false);
//				btnFilename.createH("Copy Filename", currItem.getFilename(), allButtons);
//				if (i % 2 != 0) {
//					btnFilename.setBackground(Colors.getColor(Colors.DEFAULT_ODD));
//				} else {
//					btnFilename.setBackground(Colors.getColor(Colors.DEFAULT_EVEN));
//				}
//				filePanel.add(btnFilename);
			}

			if (currItem.getTextList().size() > 0) {
				textPanel = new ItemTextPanel(grfxOrder, itemSubPanel, false);
				textPanel.setBorder(new TitledBorder(new EtchedBorder(), "TEXT"));
				itemSubPanel.add(textPanel);
				textPanel.setAlignmentX(LEFT_ALIGNMENT);
				textPanel.createPanel(currItem.getTextList());
				if (i % 2 != 0) {
					textPanel.setBackground(Colors.getColor(Colors.DEFAULT_ODD));
				} else {
					textPanel.setBackground(Colors.getColor(Colors.DEFAULT_EVEN));
				}
			}

			JPanel stillFootagePanel = new JPanel();
			stillFootagePanel.setLayout(new BoxLayout(stillFootagePanel, BoxLayout.X_AXIS));
			stillFootagePanel.setAlignmentX(LEFT_ALIGNMENT);
			itemSubPanel.add(stillFootagePanel);

			if (currItem.getStillList().size() > 0) {
				stillsPanel = new DragNDropPanel(grfxOrder, stillFootagePanel, true, DROP.STILLS);
				stillsPanel.createPanel(currItem.getStillList());
				stillsPanel.setTitle("STILLS");
				((EditableListModel)stillsPanel.list.getModel()).setExtraData(currItem.getStillList().getPath());
				stillsPanel.setLink(currItem.getStillList().getPath());
				
				stillFootagePanel.add(stillsPanel);
				stillsPanel.setAlignmentX(LEFT_ALIGNMENT);
				if (Users.currentUser.isAdmin() || Users.currentUser.isOrderer() || Users.currentUser.isPhotoEdit()) {
					stillsPanel.makeDragAndDrop(DROP.STILLS);
				}
					
				stillsPanel.addPropertyChangeListener(new ChangeListener());
					
				OrderTree2.setBackgroundIfReady(stillsPanel, currItem.getPhotoEditState());
			}

			if (currItem.getFootageList().size() > 0) {
				footagePanel = new DragNDropPanel(grfxOrder, stillFootagePanel, true, DROP.FOOTAGE);
				footagePanel.createPanel(currItem.getFootageList());
				footagePanel.setTitle("FOOTAGE");
				footagePanel.setLink(currItem.getFootageList().getPath());
				
				stillFootagePanel.add(footagePanel);
				footagePanel.setAlignmentX(LEFT_ALIGNMENT);
				if (Users.currentUser.isAdmin() || Users.currentUser.isOrderer() || Users.currentUser.isFootage()) {
					footagePanel.makeDragAndDrop(DROP.FOOTAGE);
				}
				OrderTree2.setBackgroundIfReady(footagePanel, currItem.getFootageState());
			}
		}
	}


	public class ChangeListener implements PropertyChangeListener {
		@Override
		public void propertyChange(PropertyChangeEvent arg0) {
			if(arg0.getSource()==stillsPanel){
				//Trace.info("stillspanel");
			}
			if (arg0.getPropertyName().equals("titleBody")) {
				String newTitleBody = (String) arg0.getNewValue();
				if(!newTitleBody.equals("")){
					//change the current grfxOrder
				}
			}
		}
	}
}
